//
//  SponsorsViewController.swift
//  ReadHub
//
//  Created by testypp on 2017/12/20.
//  Copyright © 2017年 longyiqun. All rights reserved.
//

import Foundation
import UIKit
import CenteredCollectionView
import UIColor_Hex_Swift

// 赞助商View
class SponsorsViewController: UIViewController {
	
	// Flow Layout
	let centeredCollectionViewFlowLayout = CenteredCollectionViewFlowLayout()
	let collectionView: UICollectionView
	
	// ControlCenterView
	let controlCenter = ControlCenterView()
	let cellPercentWidth: CGFloat = 0.3
	var scrollToEdgeEnabled = false
	
	// init
	override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
		collectionView = UICollectionView(centeredCollectionViewFlowLayout: centeredCollectionViewFlowLayout)
		super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
	}
	
	required init?(coder aDecoder: NSCoder) {
		fatalError("init(coder:) has not been implemented")
	}
	
	// 开始
	override func viewDidLoad() {
		super.viewDidLoad()
		
		view.backgroundColor = Constants.redColor
		collectionView.backgroundColor = UIColor.clear

		collectionView.delegate = self
		collectionView.dataSource = self
		
		// layout subviews
		let stackView = UIStackView()
		stackView.axis = .vertical
		stackView.addArrangedSubview(collectionView)
		stackView.addArrangedSubview(controlCenter)
		view.addSubview(stackView)
		stackView.translatesAutoresizingMaskIntoConstraints = false
		
		// 定义View的Constant
		let stackViewConstant = self.view.bounds.height / 6
		
		NSLayoutConstraint.activate([
			stackView.leadingAnchor.constraint(equalTo: view.leadingAnchor ),
			stackView.topAnchor.constraint(equalTo: view.topAnchor, constant: stackViewConstant),
			stackView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
			stackView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
			])
		
		// 注册CELL
		collectionView.register(
			CollectionViewCell.self,
			forCellWithReuseIdentifier: String(describing: CollectionViewCell.self)
		)
		
		// 每个广告位大小, 根据图片大小而来
        centeredCollectionViewFlowLayout.itemSize = CGSize(
            width: 121.0,
            height: 75.0
        )
		
		// 间隙
		centeredCollectionViewFlowLayout.minimumLineSpacing = 20
		
		// 不显示滑动条
		collectionView.showsVerticalScrollIndicator = false
		collectionView.showsHorizontalScrollIndicator = false
	}
}


// 点击每个Cell
extension SponsorsViewController: UICollectionViewDelegate {
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print("Selected Cell #\(indexPath.row)")
        if scrollToEdgeEnabled,
            let currentCenteredPage = centeredCollectionViewFlowLayout.currentCenteredPage,
            currentCenteredPage != indexPath.row {
            centeredCollectionViewFlowLayout.scrollToPage(index: indexPath.row, animated: true)
        }
    }
}

extension SponsorsViewController: UICollectionViewDataSource {
	
	// 暂时24个广告位
	func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
		return 24
	}
	
	func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
	
		let cell = collectionView.dequeueReusableCell(withReuseIdentifier: String(describing: CollectionViewCell.self), for: indexPath) as! CollectionViewCell
		
		// 获取每个广告位的图片
        cell.mainIMG.image = UIImage(named: "S" + String(indexPath.row))
		
        return cell
	}
	
//	func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
//		print("Current centered index: \(String(describing: centeredCollectionViewFlowLayout.currentCenteredPage ?? nil))")
//	}
//
//	func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {
//		print("Current centered index: \(String(describing: centeredCollectionViewFlowLayout.currentCenteredPage ?? nil))")
//	}
}

//extension UIView {
//	func applyGradient() {
//		let gradient = CAGradientLayer()
//		gradient.frame = bounds
//		gradient.locations = [0, 1]
//		gradient.startPoint = CGPoint(x: 0, y: 0)
//		gradient.endPoint = CGPoint(x: 1, y: 1)
//		gradient.colors = [
//			UIColor.gray.cgColor,
//			UIColor.green.cgColor
//		]
//		layer.insertSublayer(gradient, at: 0)
//	}
//}

//extension UIColor {
//	static var peach: UIColor {
//		return  UIColor(colorLiteralRed: 1, green: 0.5764705882, blue: 0.5843137255, alpha: 1)
//	}
//	
//	static var orange: UIColor {
//		return UIColor(colorLiteralRed: 1, green: 0.5764705882, blue: 0.462745098, alpha: 1)
//	}
//}

